package com.yanqu.road.logic.helper;

import com.yanqu.road.entity.config.system.NormalWeightItem;
import com.yanqu.road.utils.RandomHelper;

import java.math.BigInteger;
import java.util.List;
import java.util.Map;

public class WeightHelper {
    private static RandomHelper randomHelper = new RandomHelper();

    /**
     * 根据权重获取随机值
     * @param weightItemList
     * @return
     */
    public static BigInteger getRandomValue(List<NormalWeightItem> weightItemList){
        int totalWeight = 0;
        for(NormalWeightItem weightItem : weightItemList){
            totalWeight += weightItem.getWeight();
        }
        int randomValue = randomHelper.next(0, totalWeight);
        int tempValue = 0;
        for(NormalWeightItem weightItem : weightItemList){
            tempValue += weightItem.getWeight();
            if(tempValue > randomValue){
                return weightItem.getValue();
            }
        }
        return BigInteger.ZERO;
    }

    public static NormalWeightItem getRandomResult(List<NormalWeightItem> weightItemList, List<Integer> notIncludeSkillId) {
        int totalWeight = 0;
        for (NormalWeightItem weightItem : weightItemList) {
            if (notIncludeSkillId.contains(weightItem.getValue().intValue())) {
                continue;
            }
            totalWeight += weightItem.getWeight();
        }
        int randomValue = randomHelper.next(0, totalWeight);
        int tempValue = 0;
        for (NormalWeightItem weightItem : weightItemList) {
            if (notIncludeSkillId.contains(weightItem.getValue().intValue())) {
                continue;
            }
            tempValue += weightItem.getWeight();
            if (tempValue > randomValue) {
                return weightItem;
            }
        }
        return null;
    }


    public static NormalWeightItem getRandomResult(List<NormalWeightItem> weightItemList){
        int totalWeight = 0;
        for(NormalWeightItem weightItem : weightItemList){
            totalWeight += weightItem.getWeight();
        }
        int randomValue = randomHelper.next(0, totalWeight);
        int tempValue = 0;
        for(NormalWeightItem weightItem : weightItemList){
            tempValue += weightItem.getWeight();
            if(tempValue > randomValue){
                return weightItem;
            }
        }
        return null;
    }


    /**
     * 根据权重获取随机值
     * @return
     */
    public static int getRandomValue(Map<Integer, Integer> weightMap){
        int totalWeight = 0;
        for(Integer value : weightMap.values()){
            totalWeight += value;
        }
        int randomValue = randomHelper.next(0, totalWeight);
        int tempValue = 0;
        for(Map.Entry<Integer,Integer> entry : weightMap.entrySet()){
            tempValue += entry.getValue();
            if(tempValue > randomValue){
                return entry.getKey();
            }
        }
        return -1;
    }

    /**
     * 根据权重获取随机对象
     */
    public static <T> T randomT(Map<T, Integer> weightMap) {
        int totalWeight = 0;
        for (Integer value : weightMap.values()) {
            totalWeight += value;
        }
        int randomValue = randomHelper.next(0, totalWeight);
        int tempValue = 0;
        for (Map.Entry<T, Integer> entry : weightMap.entrySet()) {
            tempValue += entry.getValue();
            if (tempValue > randomValue) {
                return entry.getKey();
            }
        }
        return null;
    }

}
